package org.ljx.taobao.controller;

import org.ljx.taobao.api.IUserService;
import org.ljx.taobao.api.entity.User;
import org.ljx.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;


/**
 * @Author: Ljx
 * @Date: 2021/12/31 11:52
 * @role: 用户注册
 */
@WebServlet("/register")
public class RegisterUserServlet extends HttpServlet {
    static boolean x;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        String name = req.getParameter("names");
        String paw = req.getParameter("paw");
        String num = req.getParameter("num");

        if (Objects.equals(name, "") || Objects.equals(paw, "") || Objects.equals(num, "")){
            req.setAttribute("msg","输入的值不能为空！！！");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }else {
            User user = new User(name,paw,num);
            IUserService iUserService = new UserServiceImpl();
            try {
                x = iUserService.insertUser(user);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resp.sendRedirect("panduan4.jsp");
        }
    }
    public static boolean x() {
        return x;
    }
}

